<template>
	<view>
		<view class="top">
			<view class="title">添加银行卡</view>
			<view class="text">请绑定持卡人本人的储蓄银行卡</view>
		</view>
		<view class="intro">
			<view class="item">
				<view class="l">持卡人</view>
				<view class="r">
					<input type="text" placeholder="请输入持卡人" v-model="intro.name" />
				</view>
			</view>
			<view class="item">
				<view class="l">卡号</view>
				<view class="r">
					<input type="text" placeholder="请输入银行卡卡号" v-model="intro.bankNum" />
				</view>
			</view>
			<view class="item">
				<view class="l">卡类型</view>
				<view class="r">
					<picker mode="selector" @change="bindPickerChange" :range="array" :value="index"
						:range-key="'typeName'">
						<view class="place" v-if="index===''">请选择银行卡类型</view>
						<view v-else>{{array[index].typeName}}</view>
					</picker>
				</view>
			</view>
			<view class="item" v-if="show">
				<view class="l">开户行</view>
				<view class="r">
					<input type="text" placeholder="请填写归属银行" v-model="intro.bankName" />
				</view>
			</view>
		</view>

		<wyb-button class="btn" :ripple="true" type="filled" color="#585cf7" :radius="radius" width="100%"
			height="88rpx" font-size="32rpx" @click="addCard">确定</wyb-button>
	</view>
</template>

<script>
	import graceChecker from '@/common/graceChecker.js'
	import formRuleConfig from '@/config/formRule.config.js'
	export default {
		data() {
			return {
				radius: ["100rpx"],
				array: [],
				index: "",
				show: false,
				intro: {
					name: null,
					bankNum: null,
					tid: null,
					bankName: null
				}
			};
		},
		created() {
			this.getBankList();
		},
		methods: {
			//获取银行卡分类
			getBankList() {
				this.$api.getBankTypeList().then(res => {
					this.array = res.data.data;
				})
			},
			//picker选择事件
			bindPickerChange: function(e) {
				let index = e.target.value;
				this.index = index;
				this.show = this.array[index].flag;
				this.intro.tid = this.array[index].id;
				this.intro.bankName = "";
			},
			//添加银行卡
			addCard() {
				//判断是否选择的其他银行卡类型
				let parmas = JSON.parse(JSON.stringify(this.intro))
				if (!this.show) {
					delete parmas.bankName;
					let checkRes = graceChecker.check(parmas, formRuleConfig.addBankRule);
					if (!checkRes) {
						uni.showToast({
							title: graceChecker.error,
							icon: "none"
						});
						return;
					}
				} else {
					let checkRes = graceChecker.check(parmas, formRuleConfig.addBankRuleTwo);
					if (!checkRes) {
						uni.showToast({
							title: graceChecker.error,
							icon: "none"
						});
						return;
					}
				}
				this.$api.addBankCard(parmas).then(res => {
					uni.showToast({
						title: res.data.message,
						duration: 1000,
						icon: "success",
						success: () => {
							setTimeout(() => {
								uni.navigateBack({
									animationDuration: 2000
								})
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>
<style>
	page {
		padding: 30rpx;
		box-sizing: border-box;
	}
</style>
<style lang="scss">
	.top {
		text-align: center;
		padding: 20rpx 0 40rpx 0;
		;

		.title {
			font-weight: bold;
			font-size: 40rpx;
			margin-bottom: 20rpx;
		}

		.text {
			font-size: 32rpx;
		}
	}

	.intro {
		margin-top: 60rpx;

		.item {
			height: 110rpx;
			border-bottom: 1rpx solid #eee;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.l {
				font-size: 32rpx;
				font-weight: bold;
				width: 180rpx;
			}

			.r {
				flex: 1;
			}

			.place {
				font-size: 32rpx;
				color: #808080;
			}
		}
	}

	.btn {
		margin-top: 60rpx;
	}


</style>
